package com.edu.app.controller.core;

import com.edu.app.bean.core.SysPermission;
import com.edu.app.constant.ErrorConstant;
import com.edu.app.constant.StaticConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.service.sys.core.SysPermissionService;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.JWTUtil;
import com.edu.app.util.MsgUtil;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/api/permissions")
@RestController
public class SysPermissionController {

    @Autowired
    SysPermissionService sysPermissionService;

    /*
     * 查询User列表
     * http://localhost:8080/api/getPermissionList
     * */
    @RequestMapping(value = "")
    @RequiresAuthentication
    public ModelMap getPermissionList (){

        try{
            List<SysPermission> permissionList = sysPermissionService.getObjectList();
            return MsgUtil.retMsg(SysMessage.GET_PERMISSION_SUCCEED, StaticConstant.PERMISSION_LIST, permissionList);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @GetMapping(value = "/{id}")
    @RequiresAuthentication
    public ModelMap getPermissionById(@PathVariable Integer id){

        try{
            SysPermission sysPermission = sysPermissionService.getObjectById(id);
            return MsgUtil.retMsg(SysMessage.GET_PERMISSION_SUCCEED, StaticConstant.PERMISSION, sysPermission);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @PostMapping(value = "")
    @RequiresRoles(value = {StaticConstant.ADMIN_ROLE})
    public ModelMap addPermission(@RequestHeader("Authorization") String token, @RequestBody SysPermission sysPermission){

        try{
            String username = JWTUtil.getUsername(token);
            sysPermission.initializeSystemColumns(username);

            Integer r = sysPermissionService.add(sysPermission);
            if(r > 0){
                return MsgUtil.retMsg(SysMessage.ADD_PERMISSION_SUCCEED, null, null);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.PERMISSION_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @PutMapping(value = "")
    @RequiresRoles(value = {StaticConstant.ADMIN_ROLE})
    public ModelMap update (@RequestHeader("Authorization") String token, @RequestBody SysPermission sysPermission){

        String username = JWTUtil.getUsername(token);
        sysPermission.initializeSystemColumns(username);

        Integer i = sysPermissionService.update(sysPermission);

        if(i > 0 ){
            return MsgUtil.retMsg(SysMessage.UPDATE_PERMISSION_SUCCEED, null, null);
        }
        else{
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }

    }

    @DeleteMapping(value = "")
    @RequiresRoles(value = {StaticConstant.ADMIN_ROLE})
    public ModelMap delete (@RequestParam Integer id){

        ModelMap map = new ModelMap();
        Integer i = sysPermissionService.delete(id);
        if(i > 0 )
        {
            return MsgUtil.retMsg(SysMessage.DELETE_PERMISSION_SUCCEED, null, null);
        }
        else
        {
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }

    }
}
